(function($){
    //Add input
    $.addInput = function(t) {
        var rel = $(t).attr('rel');
        rel = '#' + rel;
        
        if ($.trim($(rel + " .last").attr('value')).length == 0) {
            $(rel + " .last").focus();
            return false;
        }
        $("div.form-error").remove();
        $(".form-error").removeClass('form-error');

        var nextInput = $(rel + " div:last").clone(true);
        var currentName = $(rel + ' .multi-input:last').attr('name');
        var currentIndex = parseInt(currentName.replace(/[\D]/g, ''));
        var nextName = currentName.replace(currentIndex, currentIndex + 1);
        var currentHiddenName = $(rel + ' .multi-input-hidden:last').attr('name');
        if (currentHiddenName != undefined) {
            var nextHiddenName = currentHiddenName.replace(currentIndex, currentIndex + 1);
        }
        $(rel + " div:last").find(".add").remove();
        $(rel).find(".last").removeClass('last');
        $(rel).append(nextInput);
        $(nextInput).find(".multi-input").attr('name', nextName).attr("value", "");
        if (nextHiddenName != undefined) {
            $(nextInput).find(".multi-input-hidden").attr('name', nextHiddenName).attr("value", "new_input");
        }
        return false;
    };

    //Remove input
    $.removeInput = function(t) {
        var rel = $(t).attr('rel');
        rel = '#' + rel;
        
        $("div.form-error").remove();
        $(".form-error").removeClass('form-error');
        
        var addSpan = $(rel).find('.add').clone(true);
        if ($(rel + ' div').length > 1) {
            $(t).parent().remove();
            $(rel + ' div:last').find(".add").remove();
            $(rel + ' div:last').append(addSpan);
            $(rel + ' div:last').find('.multi-input').removeClass('last').addClass('last');
        }
        else {
            $(t).parent().find('.multi-input').val("");
            $(t).parent().find('.multi-input-hidden').val("new_input");
        }
        return false;
    };

    $.inputLangChange = function(t) {
        var value = $(t).find('option:selected').attr('value');
        var data = {
            action: 'input_lang_change',
            lang : value
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/info-ajax.php',
            data : send,
            type : 'post',
            dataType: 'json',
            success: function(data) {
                if (data.code == 1) {
                    $(t).parents('form').find('input[name=info_name]').val(data.name);
                    $(t).parents('form').find('textarea[name=info_office]').val(data.office);
                    $(t).parents('form').find('input[name=info_position]').val(data.position);
                    $(t).parents('form').find('input[name=info_degree]').val(data.degree);
                    $(t).parents('form').find('textarea[name=info_address]').val(data.address);
                }
            }
        });
    };

    $.inputLangChangeSpecial = function(t, editType) {
        var value = $(t).find('option:selected').attr('value');
        var data = {
            action: 'input_lang_change_special',
            lang: value,
            type: editType
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/info-ajax.php',
            data : send,
            type : 'post',
            dataType: 'json',
            success: function(data) {
                if (data.code != 0) {
                    $.each(data, function(i, row){
                        $('input[value="' + row.id + '"]').siblings('.multi-input').val(row.trans);
                    });
                }
            }
        });
    };
    
    //Submit info form
    $.submitInfoForm = function(t) {
        var rel = $(t).attr('title').replace('-edit', '');
        return $.submitForm(t, function(form, data){
            if (data.code == 1) {
                $("#" + rel + "-form").hide();
                $("#" + rel + "-controller .form-close").hide();
                $("#" + rel + "-body").show();
                data = {
                    action : 'reset-' + rel + '-body'
                };
                var send = $.param(data);
                $.ajax({
                    url : 'ajax/info-ajax.php',
                    data : send,
                    dataType : 'html',
                    type : 'POST',
                    success : function(data) {
                        $("#" + rel + "-body").html(data);
                        $.resetInfoForm($(t).siblings('.info-cancel'));
                    }
                });
            }
            else {
                $(form).find(".ajax-message").htmlTimer(data.message);
            }
        });
    };

    //Reset info form
    $.resetInfoForm = function(t) {
        var rel = $(t).attr('title');
        var data = {
            action : 'reset-' + rel + '-form'
        }
        var send = $.param(data);
        $("#" + rel + "-form").find('.ajax-load').show();
        $(".form-button").addClass("disabled");
        $(".form-button").attr("disabled","disabled");

        $.ajax({
            url : 'ajax/info-ajax.php',
            data : send,
            dataType : 'html',
            type : 'POST',
            success : function(data){
                $(".form-button").removeAttr("disabled");
                $(".form-button").removeClass("disabled");
                $(".ajax-load").hide();
                $("#" + rel + "-form").html(data);
                
                //Add input
                $("#input-phones .add, #input-emails .add, #input-interests .add, #input-certifications .add, #input-academics .add").click(function(){
                    return $.addInput(this);
                });

                //Remove input
                $("#input-phones .delete, #input-emails .delete, #input-interests .delete, #input-certifications .delete, #input-academics .delete").click(function(){
                    return $.removeInput(this);
                });

                $('.input-lang-changer').change(function(){
                    $.inputLangChange(this);
                });

                $('#academics-edit .input-lang-changer-special').change(function(){
                    $.inputLangChangeSpecial(this, 'academics');
                });

                $('#interests-edit .input-lang-changer-special').change(function(){
                    $.inputLangChangeSpecial(this, 'interests');
                });

                $('#certifications-edit .input-lang-changer-special').change(function(){
                    $.inputLangChangeSpecial(this, 'certifications');
                });

                //Re-submit form
                $(".info-ajax-submit").click(function(){
                    return $.submitInfoForm(this);
                });

                //Re-Cancel form
                $(".info-cancel").click(function(){
                    return $.resetInfoForm(this);
                });
            }
        });
        return false;
    };

    $.resetAvatarBody = function() {
        var data = {
            action: 'reset_avatar_body'
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/info-ajax.php',
            type : 'POST',
            data : send,
            dataType : 'html',
            success : function(data) {
                $("#avatar-container").html(data);
            }
        });
    };

    $.resetAvatarForm = function() {
        var data = {
            action: 'reset_avatar_form'
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/info-ajax.php',
            type : 'POST',
            data : send,
            dataType : 'html',
            success : function(data) {
                $("#avatar-file-image").html(data);
            }
        });
    };

    $.submitAvatarForm = function() {
        $("#avatar-edit").ajaxSubmit({
            dataType: 'html',
            beforeSubmit: function(formParams, jqForm, options) {
                this.form = jqForm;
                $(jqForm).find(".ajax-load").show();
            },
            success: function(responseText, statusText) {
                $(this.form).find(".ajax-load").hide();
                if (responseText == "1") {
                    $.resetAvatarBody();
                    $.resetAvatarForm();
                    $("#avatar-form").hide();
                    $("#avatar-body").show();
                    $("#avatar-controller .form-close").hide();
                }
                else {
                    $(this.form).find(".ajax-message").htmlTimer(responseText, 5000);
                }
            }
        });
        return false;
    };

})(jQuery);


$(function(){
    //Toggle form
    $(".form-toggle").click(function(){
        var rel = $(this).attr('rel');
        $("#" + rel + "-body").hide();
        $("#" + rel + "-form").show();
        $("#" + rel + "-controller .form-close").show();
        return false;
    });

    $(".form-close").click(function(){
        var rel = $(this).attr('rel');
        $("#" + rel + "-body").show();
        $("#" + rel + "-form").hide();
        $("#" + rel + "-controller .form-close").hide();
        return false;
    });

    $('.input-lang-changer').change(function(){
        $.inputLangChange(this);
    });

    $('#academics-edit .input-lang-changer-special').change(function(){
        $.inputLangChangeSpecial(this, 'academics');
    });

    $('#interests-edit .input-lang-changer-special').change(function(){
        $.inputLangChangeSpecial(this, 'interests');
    });

    $('#certifications-edit .input-lang-changer-special').change(function(){
        $.inputLangChangeSpecial(this, 'certifications');
    });

    //Add input
    $("#input-phones .add, #input-emails .add, #input-interests .add, #input-certifications .add, #input-academics .add").click(function(){
        return $.addInput(this);
    });

    //Remove input
    $("#input-phones .delete, #input-emails .delete, #input-interests .delete, #input-certifications .delete, , #input-academics .delete").click(function(){
        return $.removeInput(this);
    });

    //Cancel Info form
    $(".info-cancel").click(function(){
        return $.resetInfoForm(this);
    });

    //Submit Info form
    $(".info-ajax-submit").click(function(){
        $.submitInfoForm(this);
    });

    $(".avatar-cancel").click(function() {
        $("#avatar-form").hide();
        $("#avatar-body").show();
        $("#avatar-controller .form-close").hide();
        return true;
    });

    $(".avatar-ajax-submit").click(function(){
        return $.submitAvatarForm();
    });

});


